/**
 * @file      smoke_module.c
 * @brief     smokedetect
 *
 * @author    Hi_SPARK Team
 * @date      2021/7/21
 * @version   1.0
 */

#include "smoke_module.h"
#include "hisignalling_protocol.h"

#define SMOKE_TASK_SIZE       (2048)

#define SMOKE_TASK_PRIO       (25)

hi_u32 g_smoke_id;

hi_void smoke_detect(hi_void)
{
    hi_u16 data = 0;
    hi_u32 ret = 0;
    float tem = 0;

    // ret = hi_adc_read(HI_ADC_CHANNEL_3, &data, HI_ADC_EQU_MODEL_1, HI_ADC_CUR_BAIS_AUTO, 0);
    ret = hi_adc_read(HI_ADC_CHANNEL_2, &data, HI_ADC_EQU_MODEL_1, HI_ADC_CUR_BAIS_AUTO, 0);
    if(ret != HI_ERR_SUCCESS)
    {
        return ;
    }

    ret = hi_mux_pend(g_mux_id,100);
    if (ret == HI_ERR_SUCCESS)
    {
        hisignalling_type_msg(PLUG_COMMON_HISIGNALING_SMOKE, data);
        hi_mux_post(g_mux_id);
    }
    printf("data = %d\r\n",data);
    

}




static hi_void smoke_thread(hi_void)
{
    hi_u32 ret;
    // hi_io_set_func(HI_IO_NAME_GPIO_7, HI_IO_FUNC_GPIO_7_GPIO);
    // hi_gpio_set_dir(HI_GPIO_IDX_7, HI_GPIO_DIR_IN);
    hi_io_set_func(HI_IO_NAME_GPIO_5, HI_IO_FUNC_GPIO_5_GPIO);
    hi_gpio_set_dir(HI_GPIO_IDX_5, HI_GPIO_DIR_IN);
    for(;;) 
    {
        smoke_detect();
        hi_sleep(1000);
    }
    /*Delete task*/
    ret = hi_task_delete(g_smoke_id);
    if (ret != HI_ERR_SUCCESS) {
        printf("Failed to smoke_thread\r\n");
    }

}



hi_u32 smoke_task(hi_void)
{
    hi_u32 ret;
    hi_task_attr smoke_attr = {0};
    
    smoke_attr.stack_size = SMOKE_TASK_SIZE;//Task stack memory
    smoke_attr.task_prio = SMOKE_TASK_PRIO;//The task priority ranges from 0 to 31. Tasks 0 to 10 should not be used. The SDK has been used. The higher the value, the lower the priority
    smoke_attr.task_name = (hi_char*)"smoke  control";//Task name
	//Create task
    ret = hi_task_create(&g_smoke_id, &smoke_attr, smoke_thread, HI_NULL);
    if (ret != HI_ERR_SUCCESS) {
        printf("Failed to create smoke_thread\r\n");
        return HI_ERR_FAILURE;
    }
    return HI_ERR_SUCCESS;
}





























